package com.easidea.tim.dao.finance;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springside.core.dao.extend.HibernateEntityExtendDao;

import com.easidea.tim.dto.ShouldPaySummaryDTO;
import com.easidea.tim.entity.finance.ShouldPaySummary;
import com.easidea.tim.entity.purchase.PurchaseOrder;
import com.easidea.tim.entity.supply.Sup;
import com.easidea.tim.util.DateUtil;
import com.easidea.tim.util.Page;

public class ShouldPaySummaryDao extends HibernateEntityExtendDao<ShouldPaySummary> {
 public ShouldPaySummary getShouldPaySummaryBySupId(long supId) {
	 Criteria ca = this.createUnDeletedCriteria();
	  ca.add(Restrictions.eq("supplier.id", supId)); 
	  return (ShouldPaySummary) ca.uniqueResult();
 }
	/**
	 * 根据条件查询应付款列表
	 * @param startDate
	 * @param endDate
	 * @param flowNo
	 * @param page
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public Page getShouldPayListByCondition(
			Date startDate, Date endDate, 
			String supName,String responser,Page page) {
		Page resultPage = null;
		String hql = " select pay.supplier.name as supName ,pay.supplier.id as supId, SUM(pay.amount) as amount,"
			 + " SUM(pay.payed) as payed, SUM(pay.amount)  - SUM(pay.payed) as balance  from "
			+ ShouldPaySummary.class.getName()+ " as pay";
		String where = "  where 1 = 1 ";
		String groupby = " group by pay.supplier.name, pay.supplier.id ";
		if(startDate != null && !startDate.equals("") && endDate != null && !endDate.equals("") ) {
			where += " and pay.shouldPayList.billDate >= '"+DateUtil.formatDateTime(DateUtil.getStartTime(startDate)) +"' and pay.shouldPayList.billDate <= '" + DateUtil.formatDateTime(DateUtil.getEndTime(endDate)) +"'";
		}
		if(supName != null && !supName.equals("")){
			where += " and pay.supplier.name like '"+supName+"%'";
		}
		if(responser != null && !responser.equals("")){
			where += " and pay.supplier.responser like '"+responser+"%'";
		}		
		hql += where + groupby; 
		Query q1 = this.getSession().createQuery(hql);
		Integer totalNum = q1.list().size();
		int startIndex = Page.getStartOfPage(page.getCurrentPageNo(), page.getPageSize());
		if(totalNum > 0) { 	
			q1.setFirstResult(startIndex);
			q1.setMaxResults(page.getPageSize());	
			List<Object[]> ls = (List<Object[]>)q1.list();
			List<ShouldPaySummaryDTO> pays = new ArrayList<ShouldPaySummaryDTO>();
			for (Object[] obj : ls) {
				pays.add(new ShouldPaySummaryDTO((String)obj[0],(Long)obj[1],((Double)obj[2]),
						((Double)obj[3]),((Double)obj[4])));
			}
				resultPage = new Page(startIndex, totalNum, page.getPageSize(), pays);
				resultPage.setCurrentPageNo(page.getCurrentPageNo());
		} else {
			resultPage = new Page(startIndex, totalNum, page.getPageSize(), new ArrayList());
			resultPage.setCurrentPageNo(page.getCurrentPageNo());
		}
		return resultPage;
	} 
	
	/**
	 * @author Harrison
	 * @param order
	 * @return
	 */
	public ShouldPaySummary saveOrUpdateShouldPaySummary(PurchaseOrder order) {
		 ShouldPaySummary paySummary = null;
			paySummary = getShouldPaySummaryBySupId(order.getSupplier().getId());
			if (paySummary == null) {
				paySummary = new ShouldPaySummary();
			}
			paySummary.setAmount(paySummary.getAmount() == null ?(0+order.getTotalAmount()): (paySummary.getAmount() + order.getTotalAmount()));
			paySummary.setPayed(paySummary.getPayed() == null ? 0.0 : paySummary.getPayed());
			paySummary.setSupplier(order.getSupplier());
			
			getHibernateTemplate().saveOrUpdate(paySummary);
			return paySummary;
	 }
	
	
	/**
	 * 根据供应商ID和金额进行应付款总额的修正
	 * @param suppId
	 * @param amount
	 * @return
	 */
	public ShouldPaySummary saveOrUpdateShouldPaySummary(Long suppId, Double amount){
		 ShouldPaySummary paySummary = null;
			paySummary = getShouldPaySummaryBySupId(suppId);
			if (paySummary == null) {
				paySummary = new ShouldPaySummary();
			}
			
			Sup supplier = new Sup();
			supplier.setId(suppId);
			
			paySummary.setAmount(paySummary.getAmount() == null ?(amount): (paySummary.getAmount() + amount));
			paySummary.setPayed(paySummary.getPayed() == null ? 0.0 : paySummary.getPayed());
			paySummary.setSupplier(supplier);
			
			getHibernateTemplate().saveOrUpdate(paySummary);
			return paySummary;
	}
}
